package work.shuangzhizhen;

/**
 * @author WDH
 * @description
 * @date 2024年10月16日 15:42
 */

public class Problem121 {

    public int maxProfit(int[] prices) {
        int pre = 0;
        int tail = pre;
        int max = 0;
        if (prices.length == 1) {
            return 0;
        }
        while (tail != prices.length - 1) {
            tail = tail + 1;
            if (prices[tail] > prices[pre]) {
                int result = prices[tail] - prices[pre];
                max = Math.max(result, max);
            } else {
                pre = tail;
            }
        }

        return max;
    }

    public static void main(String[] args) {
        Problem121 problem121 = new Problem121();
        int[] prices = {1, 2};
        int i = problem121.maxProfit(prices);
        System.out.println(i);
    }

    public int maxProfit1(int[] prices) {
        int pre = 0, tail = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < prices[pre]) {
                pre = i;
            }
            if (prices[i]>prices[pre]){
            tail = Math.max(tail,prices[i]-prices[pre]);}
        }
        return tail;
    }

    public int maxProfit3123(int[] prices) {
        int ans = 0;
        int n = prices.length;
        int sum = 0;
        for (int i = 1; i <n ; i++) {
            if (prices[i]<prices[ans]) ans = i;
            sum =Math.max(sum,prices[i]-prices[ans]);
        }
        return sum;

    }

}
